<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>老师主页</title>
    <link rel="stylesheet" href="layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<div class="layui-form-item">
    <div class="layui-inline">
        <label class="layui-form-label">序号：</label>
        <div class="layui-input-inline">
            <input type="text" name="tid" id="tidReload" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">账号：</label>
        <div class="layui-input-inline">
            <input type="text" name="username" id="usernameReload" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">姓名：</label>
        <div class="layui-input-inline">
            <input type="text" name="name"  id="nameReload" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">职位：</label>
        <div class="layui-input-inline">
            <input type="text" name="career"  id="careerReload" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">电话：</label>
        <div class="layui-input-inline">
            <input type="text" name="phone"  id="phoneReload" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">地址：</label>
        <div class="layui-input-inline">
            <input type="text" name="address"  id="addressReload" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">打卡状态：</label>
        <div class="layui-input-inline">
            <input type="text" name="clock"  id="clockReload" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button type="submit" class="layui-btn" data-type="reload" id="search">搜索</button>
            <button class="layui-btn"  type="reset" id="reset">重置</button>
        </div>
    </div>
</div>



<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addTeacher">增加</button>
        <!--<button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>-->
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<!--<script src="layui/layui.js" charset="utf-8"></script>-->
<script th:src="@{layui/layui.js}" charset="utf-8"></script>
<script th:src="@{lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

<script type="text/javascript" src="layui/jquery.min.js"></script>

<script>
    layui.use(['table','form'], function(){
        var table = layui.table;
        var form = layui.form
        //温馨提示：默认由前端自动合计当前行数据。从 layui 2.5.6 开始： 若接口直接返回了合计行数据，则优先读取接口合计行数据。
        //详见：https://www.layui.com/doc/modules/table.html#totalRow
        table.render({
            elem: '#test'  //绑定元素
            ,url:'/teacher'  //数据来源
            ,toolbar: '#toolbarDemo'   //映射绑定
            ,title: '老师个人信息数据表'   //导出数据所使用的的名单
            ,totalRow: true
            ,limits:[10,20,40,80,30000]
            ,cols: [
                [
                    {type: 'checkbox', fixed: 'left'}
                    ,{field:'tid', title:'序号', width:80, fixed: 'left', unresize: true, sort: true, totalRowText: '合计'}
                    ,{field:'username', title:'工号(账号)', width:130, sort: true}
                    ,{field:'password', title:'密码', width:100}
                    ,{field:'name', title:'姓名', width:80}
                    ,{field:'career', title:'职业', width:80}
                    ,{field:'phone', title:'手机号', width:130, sort: true}
                    ,{field:'address', title:'居住地址', width:250}
                    ,{field:'status', title:'状态', width:80,
                            templet:function(res){
                                if(res.status==='停用'){
                                    return '<a class="layui-btn layui-btn-danger layui-btn-xs">停用</a>';
                                }else if(res.status==='正常'){
                                    return '<a class="layui-btn layui-btn-xs">正常</a>';
                                }
                            }
                        }
                    ,{field:'clock', title:'是否打卡', width:100,
                            templet:function(res){
                                if(res.clock==='未打卡'){
                                    return '<a class="layui-btn layui-btn-danger layui-btn-xs">未打卡</a>';
                                }else if(res.clock==='已打卡'){
                                    return '<a class="layui-btn layui-btn-xs">已打卡</a>';
                                }
                            }
                        }
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
                ]
            ]
            ,page: true //分页
            ,id:'teacherTable'
        });

        var $ = layui.$,active = {
            reload:function(){
                var tidReload = $('#tidReload');
                var usernameReload = $('#usernameReload');
                var nameReload = $('#nameReload');
                var careerReload = $('#careerReload');
                var phoneReload = $('#phoneReload');
                var addressReload = $('#addressReload');
                var clockReload = $('#clockReload');

                var index = layer.msg("查询中，请稍后...",{
                    icon:16,
                    time:false,
                    shade:0
                });
                setTimeout(function(){
                    table.reload('teacherTable',{
                        page:{
                            curr:1
                        }
                        ,where:{
                            /*key:{*/
                            tid:tidReload.val(),
                            username:usernameReload.val(),
                            name:nameReload.val(),
                            career:careerReload.val(),
                            phone:phoneReload.val(),
                            address:addressReload.val(),
                            clock:clockReload.val()
                            /*}*/
                        },
                        method:'POST',
                        url:'teacher/searchTeacher'
                    });
                    layer.close(index);
                },800);

            }
        };

        $('#search').on('click',function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this):'';
        })

        $('#reset').on('click',function(){
            $('#tidReload').val('');
            $('#usernameReload').val('');
            $('#nameReload').val('');
            $('#careerReload').val('');
            $('#phoneReload').val('');
            $('#addressReload').val('');
            $('#clockReload').val('');
        });



        //工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){   //当前点击
                /*case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;*/
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选')
                    break;
                case 'addTeacher':
                    var index = layer.open({
                        title: '添加用户',
                        type: 2, //0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                        shade:0.2,
                        maxmin: true,
                        shadeClose : true,
                        area :['80%','80%'],
                        content: '/addTeacher',
                    });
                    $(window).on("resize",function(){
                        layer.full(index)
                    });
                    return false;
                    break;

            };
        });

        //监听工具条
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    //直接向服务器发送删除请求
                    $.ajax({
                        url:'teacher/deleteTeacherById',
                        type:'POST',
                        data:{tid:data.tid},
                        success: function(result){
                            if(result.code===200){
                                layer.close(index);
                                layer.msg("删除成功！");
                                window.location.reload();
                            }else{
                                layer.msg("删除失败！");
                            }
                        }
                    })
                });
            } else if(obj.event === 'edit'){
                layer.msg("修改"+data.tid);
                //打开弹出层
                var index = layer.open({
                    type: 1,
                    title: "修改用户",
                    shade:0.2,
                    maxmin: true,
                    shadeClose : true,
                    area :['80%','80%'],
                    content: $("#teacherUpdateTest")
                });
                $(window).on("resize",function(){
                    layer.full(index)
                });
                //方式1通过Id查询该记录，方式2，获取该选中记录，然后data渲染。
                setFormValue(obj,data);
            }
        });

        function setFormValue(obj,data){
            console.log(data);
            data.admin = "0";
            if(data.status==="停用"){
                data.status='';
            }

            console.log(data);
            form.val("updateBtn",{
                "tid":data.tid,
                "username":data.username,
                "password":data.password,
                "name":data.name,
                "career":data.career,
                "phone":data.phone,
                "address":data.address,
                "status":data.status,
                "clock":data.clock
            });
            form.on('submit(updateBtn)',function(data){
                var datas = data.field;
                console.log(datas);
                if(datas.status==="on"){
                    datas.status=1;
                }
                //向后端发送数据并进行添加操作
                $.ajax({
                    url:"teacher/updateTeacherSubmit",
                    type: "POST",
                    data:datas,
                    success:function(result){
                        //将json对象转成string
                        // result = JSON.parse(result);
                        if(result.code===200){
                            layer.msg("修改成功！",{
                                icon:6,
                                time:500
                            },function(){
                                window.location.reload();//重新加载页面
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                            });
                        }else{
                            layer.msg("修改失败！");
                        }
                    }
                })

            })


        };


    });
</script>

<div class="layui-form layuimini-form" id="teacherUpdateTest" style="display:none" lay-filter="updateBtn">
    <input type="hidden" name="tid" />
    <div class="layui-form-item">
        <label class="layui-form-label required">用户账号</label>
        <div class="layui-input-block">
            <input type="text" name="username" lay-verify="required" lay-reqtext="账号不能为空" placeholder="请输入用户名" value="" class="layui-input">
            <!--<tip>填写自己管理账号</tip>-->
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">用户密码</label>
        <div class="layui-input-block">
            <input type="text" name="password" lay-verify="required" lay-reqtext="密码不能为空" placeholder="请输入密码" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">职位</label>
        <div class="layui-input-block">
            <input type="text" name="career" lay-verify="required" lay-reqtext="职位不能为空" placeholder="请输入职位" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">手机号</label>
        <div class="layui-input-block">
            <input type="text" name="phone" lay-verify="required" lay-reqtext="手机号不能为空" placeholder="请输入手机号" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">现住地址</label>
        <div class="layui-input-block">
            <input type="text" name="address" lay-verify="required" lay-reqtext="现住地址不能为空" placeholder="请输入现住地址" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-input">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="status" lay-skin="switch" lay-text="正常|停用">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">是否打卡</label>
        <div class="layui-input-block">
            <input type="radio" name="clock" value="未打卡" title="未打卡">
            <input type="radio" name="clock" value="已打卡" title="已打卡">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="updateBtn">确认修改</button>
        </div>
    </div>
</div>




</body>
</html>